package com.xyhuo.leetcode.june;

import com.xyhuo.leetcode.common.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * 173. 二叉搜索树迭代器
 * https://leetcode.cn/problems/binary-search-tree-iterator/
 */
public class BSTIterator {

    private int index;

    private List<Integer> list;

    public BSTIterator(TreeNode root) {
        index = 0;
        list = new ArrayList<>();
        middleList(root);
    }

    private void middleList(TreeNode root) {
        if(null==root){
            return;
        }
        middleList(root.left);
        list.add(root.val);
        middleList(root.right);
    }

    public int next() {
        if (index >= list.size()) {
            return -1;
        }
        return list.get(index++);
    }

    public boolean hasNext() {
        if (index >= list.size()) {
            return false;
        }
        return true;
    }
}
